package progress.exa28_1.part5;

/**
 * 线程安全的类
 * 同步阻塞Account
 */
public class SynBlockAccount implements Runnable {

    private int money;

    private static int age;


    public void deposit(int i) throws InterruptedException {
        Thread.sleep(100);
        synchronized (this){
            money += i;
        }
        Thread.sleep(100);
    }

    public void ageAdd(int i) throws InterruptedException {
        Thread.sleep(100);
        synchronized (this){
            age += i;
        }
        Thread.sleep(100);
    }


    @Override
    public void run() {
        try {
            Thread.sleep(100);
            deposit(5);
            ageAdd(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "Account{" +
                "money=" + money +
                " age="+ age+
                '}';
    }


}
